package fileInterface 
{
	import com.greensock.plugins.AutoAlphaPlugin;
	import com.greensock.plugins.TweenPlugin;
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import SWFKit.Shell;
	/**
	 * ...
	 * @author GertS
	 */
	public class MenuButton extends Sprite
	{

		public var tooltip:String;
		private var _holderRotation:Number;
		private var _externalAction:String;
		private var _internalAction:String;
		private var _fileInterFace:fileInterface.FileInterface;
		
		public function MenuButton(fileInterface_:fileInterface.FileInterface, iconUrl:String, tooltip_:String, externalAction_:String, internalAction_:String) : void {
			
			TweenPlugin.activate([AutoAlphaPlugin]);
			
			_fileInterFace = fileInterface_;
			_externalAction = externalAction_;
			_internalAction = internalAction_;
			tooltip = tooltip_;
			super();
			var bitmapLoader:Loader = new Loader()
			bitmapLoader.load(new URLRequest(iconUrl));
			bitmapLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBitmapComplete);
			
			
			addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 1000);
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			//new Shell().runConsole("assets\\run.bat " + parseTags(_externalAction));
			new Shell().run("assets\\run.bat " + parseTags(_externalAction), 0);
			if (_internalAction.length > 0) {
				this[_internalAction]();
			}
		}
		
		private function parseTags(s:String):String
		{
			s = s.split("{fileName}").join(_fileInterFace.fileName);
			s = s.split("{folderPath}").join(_fileInterFace.folderPath);
			return s;
		}
		
		private function rollOverHandler(e:MouseEvent):void 
		{
			_fileInterFace.showTooltip(x, y, tooltip);
			//parent.setChildIndex(this,parent.numChildren - 1);
		}
		
		private function rollOutHandler(e:MouseEvent):void 
		{
			_fileInterFace.hideTooltip();
		}
		
		private function onBitmapComplete(e:Event):void 
		{
			var iconHolder:Sprite = new IconHolder();
			addChild(iconHolder);
			iconHolder.name = "iconHolder";
			iconHolder.rotation = _holderRotation;
			var bitmap:Bitmap = Bitmap(LoaderInfo(e.target).content);
			bitmap.smoothing = true;
			bitmap.pixelSnapping = PixelSnapping.NEVER;
			bitmap.x = -bitmap.width / 2
			bitmap.y = -bitmap.height / 2
			addChild(bitmap);
		}
		
		private function deleteImage():void {
			SnippingTool.box2DHolder.removeChild(_fileInterFace.bodySprite);
			SnippingTool._world.DestroyBody(_fileInterFace.body);
		}
		
		public function get holderRotation():Number { return _holderRotation; }
		
		public function set holderRotation(value:Number):void 
		{
			_holderRotation = (value * 180 / Math.PI ) + 90;
			getChildByName("iconHolder").rotation = _holderRotation;
		}
		
	}

}